#include "HPdfView.h"
#include "PdfLib/QPdfWidget.h"
#include "ui_HPdfView.h"

#include <QHBoxLayout>

HPdfView::HPdfView(QWidget *parent)
    : QMainWindow(parent)
{
    ui = new Ui::HPdfViewClass();
    ui->setupUi(this);

    m_pPdfWidget = new QPdfWidget(this);
    m_pPdfWidget2 = new QPdfWidget(this);
    m_pPdfWidgetLayout = new QHBoxLayout();
    m_pPdfWidgetLayout2 = new QHBoxLayout();
    Init();
    InitSignalAndSlot();

    this->OnOpen();
}

HPdfView::~HPdfView()
{
    if (m_pPdfWidget)
    {
        delete m_pPdfWidget;
        m_pPdfWidget = nullptr;
    }
    if (m_pPdfWidgetLayout)
    {
        delete m_pPdfWidgetLayout;
        m_pPdfWidgetLayout = nullptr;
    }
    if (m_pPdfWidget2)
    {
        delete m_pPdfWidget2;
        m_pPdfWidget2 = nullptr;
    }
    if (m_pPdfWidgetLayout2)
    {
        delete m_pPdfWidgetLayout2;
        m_pPdfWidgetLayout2 = nullptr;
    }
}

void HPdfView::Init()
{
    m_pPdfWidgetLayout->setContentsMargins(0, 0, 0, 0);
    m_pPdfWidgetLayout->setSpacing(0);
    m_pPdfWidgetLayout->addWidget(m_pPdfWidget);
    ui->view1->setLayout(m_pPdfWidgetLayout);

    m_pPdfWidgetLayout2->setContentsMargins(0, 0, 0, 0);
    m_pPdfWidgetLayout2->setSpacing(0);
    m_pPdfWidgetLayout2->addWidget(m_pPdfWidget2);
    ui->view2->setLayout(m_pPdfWidgetLayout2);
}

void HPdfView::InitSignalAndSlot()
{
    //connect(m_pPdfWidget, SIGNAL(LoadFinished()), this, SLOT(OnLoadFinished()));
    //connect(m_pPdfWidget, SIGNAL(pageNumberChanged(int)), this, SLOT(OnPageNumberChanged(int)));
    //connect(m_pPdfWidget, SIGNAL(pageCountChanged(int)), this, SLOT(OnPageCountChanged(int)));
    //connect(m_pPdfWidget, SIGNAL(pageScaleChanged(double)), this, SLOT(OnPageScaleChanged(double)));
    connect(m_pPdfWidget, &QPdfWidget::LoadFinished, this, &HPdfView::OnLoadFinished);
    connect(m_pPdfWidget, &QPdfWidget::pageNumberChanged, this, &HPdfView::OnPageNumberChanged);
    connect(m_pPdfWidget, &QPdfWidget::pageCountChanged, this, &HPdfView::OnPageCountChanged);
    connect(m_pPdfWidget, &QPdfWidget::pageScaleChanged, this, &HPdfView::OnPageScaleChanged);

    connect(m_pPdfWidget2, &QPdfWidget::LoadFinished, this, &HPdfView::OnLoadFinished2);
    connect(m_pPdfWidget2, &QPdfWidget::pageNumberChanged, this, &HPdfView::OnPageNumberChanged);
    connect(m_pPdfWidget2, &QPdfWidget::pageCountChanged, this, &HPdfView::OnPageCountChanged);
    connect(m_pPdfWidget2, &QPdfWidget::pageScaleChanged, this, &HPdfView::OnPageScaleChanged);


}

void HPdfView::OnOpen()
{

}

void HPdfView::OnLoadFinished()
{
    QString strFile = "Report_OB.pdf";
    m_pPdfWidget->LoadFile(strFile);
}

void HPdfView::OnLoadFinished2()
{
    QString strFile = "Report_OB.pdf";
    m_pPdfWidget2->LoadFile(strFile);
}

// 当前页数
void HPdfView::OnPageNumberChanged(int nPageNumber) {}

// 总页数
void HPdfView::OnPageCountChanged(int nPageCount) {}

//页面大小发生变化
void HPdfView::OnPageScaleChanged(double fScale) {}